﻿using Mirror;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;

public class ShipFactory : NetworkBehaviour
{
    public string m_ProductionInfluence;
    public GameObject m_ProductioMachineType;
    public GameObject m_ProductioHeadquarters;
    public GameObject m_ProductionPoint;
    public GameObject m_Target;
    public float m_ProductionFrequency = 7;

    float m_ProductionFrequencyTimeCount=0;

    List<Ship> m_ProductList = new List<Ship>();
    void Start()
    {
        m_ProductionFrequencyTimeCount = m_ProductionFrequency;
    }

    // Update is called once per frame
    void Update()
    {
        if (UtilsGame.IsOnline() && !UtilsGame.IsServer(gameObject))
            return;
        if (m_ProductList.Count > 5)
            return;
        m_ProductionFrequencyTimeCount += Time.deltaTime;
        if (m_ProductionFrequencyTimeCount < m_ProductionFrequency)
            return;
        m_ProductionFrequencyTimeCount = 0;

        //  生产一个飞机
        if (null == m_ProductioHeadquarters)
            return;
        GameObject ship = Instantiate(m_ProductioMachineType) as GameObject;
        ship.transform.SetParent(m_ProductioHeadquarters.transform);

        var shipCom = ship.GetComponent<Ship>();
        var headquartersCom = m_ProductioHeadquarters.GetComponent<Headquarters>();

        shipCom.m_ShipStruct.Influence = m_ProductionInfluence;
        m_ProductList.Add(shipCom);
        headquartersCom.Target = m_Target;
        if (m_ProductionPoint)
            ship.transform.position = m_ProductionPoint.transform.position;

        if (UtilsGame.IsServer(gameObject))
            UtilsGame.SpawnInServer(ship);
    }

    private void FixedUpdate()
    {
        List<Ship> productList = new List<Ship>();
        foreach (var shipi in m_ProductList)
        {
            if(shipi != null)
                productList.Add(shipi);
        }
        m_ProductList = productList;
    }
}
